#! /usr/bin/python
# -*- coding: cp1250 -*-

import glob
import re
import string
import sys
import os
import pepe.p_txt.bigtxt as bt

class Select:
    """ """
    def __init__(self):
        """ """
        self.inputfilemask = ''
        self.outputfilename = ''
        self.replace = ['','']
        self.skip_rows = 0
        self.skip_emptyrows = 1
        self.keep_header = 0
        self.keep_regex = ['']
        self.drop_regex = ['']
        self.cdelimiter = '\t'
        self.cdelimiter_output = '\t'
        self.keep_columns = ['']
        self.lino = 0
        self.emptyrowscnt = 0
        self.dropped_lines_file = 1

    def outputRows(self):
        """ """
        self.emptyrowscnt = 0
        #of = open(self.outputfile,'w')

        if self.outputfilename <> '':
            of = open(self.outputfilename,'wb')

        for infile in glob.glob(self.inputfilemask):
            # vp 2010-09-12_1201
            infname = string.split(infile, '.')[0]
            infname_ext = string.split(infile, '.')[-1]
            #of = open(infname + '.p-utils-txt.' + infname_ext,'w')

            # 2010-10-22_2257 vp
            ## using underscore instead dot because .# utilities 
            ## cut out everything after dot and then it is not clear 
            ## whether utility was run on original file or on output
            ## of other .# utility that processed given file
            #of = open(infname + '_p-utils-txt.#','w')
            if self.outputfilename == '':
                of = open('txt_' + infname, 'wb')

            # dropped lines file
            if self.dropped_lines_file == 1:
                # see above
                #of_dropped = open(infname + '.p-utils-txt.#_dropped_lines','w')
                of_dropped = open(infname + '_p_utils_txt_dropped_lines','wb')

            self.lino = 0

            for line in bt.lines(infile):
                line = re.sub('\x0D\x0A', '', line)
                self.lino += 1
                write = 1

                #print '-',re.sub('\s{1,}','', line),'-'
                if self.skip_emptyrows == 1 and re.sub('\s{1,}','', line) == "": self.emptyrowscnt += 1
                else:
                    # keep if regex
                    if len(self.keep_regex) <> 1 or self.keep_regex <> [''] :
                        for keep_rx in self.keep_regex:
                            n = re.search(keep_rx, line)
                            if n: 
                                write = 1
                            else: write = 0
        
                    # drop if regex
                    if len(self.drop_regex) <> 1 or self.drop_regex <> [''] :
                        for drop_rx in self.drop_regex:
                            n = re.search(drop_rx, line)
                            if n: write = 0
        
                    # header
                    if self.keep_header == 1 and self.lino == 1: write = 1
        
                    # replace
                    if self.replace <> ['','']: line = re.sub(self.replace[0], self.replace[1], line)
                    
                    # skip rows 
                    if self.skip_rows >= self.lino: write = 0
                    
                    if write == 1:
                        of.write(line+'\x0D\x0A')
                    else:
                        if self.dropped_lines_file == 1:
                            of_dropped.write(line+'\x0D\x0A')

        of.close()
        of_dropped.close()
        self.lino = 0
        if self.emptyrowscnt <> 0: print 'self.emptyrowscnt = :', str(self.emptyrowscnt)
        self.emptyrowscnt = 0

    def outputColumns(self):
        """ """
        self.emptyrowscnt = 0
        #print 'column delimiter set to:', '"', self.cdelimiter, '"'
        #of = open(self.outputfile,'w')

        if self.outputfilename <> '':
            of = open(self.outputfilename,'wb')

        for infile in glob.glob(self.inputfilemask):
        
            # vp 2010-09-12_1201
            infname = string.split(infile, '.')[0]
            infname_ext = string.split(infile, '.')[-1]
            
            # 2010-10-22_2257 vp
            ## using underscore instead dot because .# utilities 
            ## cut out everything after dot and then it is not clear 
            ## whether utility was run on original file or on output
            ## of other .# utility that processed given file
            #of = open(infname + '.p-utils-txt.' + infname_ext,'w')
            #of = open(infname + '.p-utils-txt.#','w')
            #of = open(infname + '.p-utils-txt.' + infname_ext,'w')
            if self.outputfilename == '':
                of = open('txt_' + infname,'wb')
            efie = open('txt_IndexError_' + infname,'wb')
            
            self.lino = 0

            for line in bt.lines(infile):
                line = re.sub('\x0D\x0A', '', line)
                self.lino += 1
                write = 1

                if self.skip_emptyrows <> 0 and re.sub('\s{1,}','', line) == "": self.emptyrowscnt += 1
                else:
                    cols = string.split(line, self.cdelimiter)
                    #print line
                    #print cols
                    #print len(cols)
                    res = []
                    if self.keep_columns <> ['']:
                        for c in self.keep_columns:
                            try:
                                # 2011-09-20_0703 vip
                                res.append(string.strip(cols[c]))
                            except IndexError:
                                efie.write('line number: %s line: %s \x0D\x0A' % (self.lino, line))
        
                    # skip rows 
                    if self.skip_rows >= self.lino: write = 0
        
                    if write == 1: of.write(string.join(res, self.cdelimiter_output)+'\x0D\x0A')

        of.close()
        efie.close()
        self.lino = 0
        if self.emptyrowscnt <> 0: print 'self.emptyrowscnt = :', str(self.emptyrowscnt)
        self.emptyrowscnt = 0

EXAMPLE = """

import pepe.p_utils.txt as ppt

t = ppt.Select()

## example of keep/drop rows

t.inputfilemask = 'a.dat'
t.keep_header = 1
t.skip_emptyrows = 1
t.dropped_lines_file = 1
#t.keep_regex = ['^\d{1,}100']
#t.keep_regex.append('...')
t.drop_regex = ['^\d{1,}100']
t.drop_regex.append('...')
t.replace = ['\t','^']
t.outputRows()

## example of replace

t.inputfilemask = 'a01Rows.log'
t.replace = [',','']
t.outputRows()

## example of replace advanced

t.inputfilemask = 'a02Rows.log'
t.replace = ['\.(?P<A>\d{2})',',\g<A>']
t.outputRows()

## example of keep/drop columns

t.inputfilemask = 'a.dat'
t.cdelimiter = '\t'
t.keep_columns = [0,3]
t.outputColumns()

## example of joining multiple files

t.inputfilemask = '*.dat'
t.skip_rows = 5
t.outputRows()

"""

if __name__ == "__main__":
    """ """
    of = open('_txt.py','w')
    # write template file with example in current dir
    of.write(EXAMPLE)
    of.close()

